package io.renren.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.entity.XlsAreasEntity;
import io.renren.common.entity.XlsUserEntity;
import io.renren.common.entity.XlsUsersAddressEntity;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.dao.XlsAreasDao;
import io.renren.dao.XlsUserDao;
import io.renren.dao.XlsUsersAddressDao;
import io.renren.dto.XlsUsersAddressDTO;
import io.renren.service.XlsUsersAddressService;
import io.renren.utils.AppResult;
import io.renren.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 会员地址表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2020-04-12
 */
@Service
public class XlsUsersAddressServiceImpl extends CrudServiceImpl<XlsUsersAddressDao, XlsUsersAddressEntity, XlsUsersAddressDTO> implements XlsUsersAddressService {

    @Autowired
    private XlsUsersAddressDao xlsUsersAddressDao;
    @Autowired
    private XlsUserDao xlsUserDao;
    @Autowired
    private XlsAreasDao areasDao;

    @Override
    public QueryWrapper<XlsUsersAddressEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<XlsUsersAddressEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public AppResult selectAddressByuserId(Long userId) {
        JSONArray jsonArray=new  JSONArray();
        XlsUserEntity users = xlsUserDao.selectById(userId);
        if (StringUtils.isNull(users)){
            return AppResult.ERROR("无效用户");
        }
        XlsUsersAddressEntity usersAddress=new XlsUsersAddressEntity();
        usersAddress.setUserId(userId);
        usersAddress.setDataFlag(1);
        List<XlsUsersAddressEntity> addresss = xlsUsersAddressDao.selectUsersAddressList(usersAddress);
        if (!addresss.isEmpty()){
            for (XlsUsersAddressEntity usersAddress1:addresss){
                JSONObject json=new JSONObject();
                Long addressId = usersAddress1.getId();
                String userName = usersAddress1.getUserName();
                String userPhone = usersAddress1.getUserPhone();
                Integer provinceId = usersAddress1.getProvinceId();
                XlsAreasEntity areas = areasDao.selectById(provinceId);
                Integer cityId = usersAddress1.getCityId();
                XlsAreasEntity areas1 = areasDao.selectById(cityId);
                Integer areaId = usersAddress1.getAreaId();
                XlsAreasEntity areas2 = areasDao.selectById(areaId);
                String userAddressName = areas.getAreaName()+areas1.getAreaName()+areas2.getAreaName()+usersAddress1.getUserAddress();
                json.put("addressId",addressId);
                json.put("userName",userName);
                json.put("userPhone",userPhone);
                json.put("userAddressName",userAddressName);
                jsonArray.add(json);
            }
        }
        return AppResult.OK("查询成功",jsonArray);
    }

    @Override
    @Transactional
    public AppResult createAddress(XlsUsersAddressEntity usersAddress) {
        Long userId = usersAddress.getUserId();
        XlsUsersAddressEntity usersAddress1=new XlsUsersAddressEntity ();
        usersAddress1.setUserId(userId);
        List<XlsUsersAddressEntity> addresss = xlsUsersAddressDao.selectUsersAddressList(usersAddress1);
        if (addresss.isEmpty()) {
            usersAddress.setIsDefault(1);
            usersAddress.setDataFlag(1);
            usersAddress.setIsUpdate(-1);
            usersAddress.setCreateTime(new Date());
            xlsUsersAddressDao.insert(usersAddress);
            return  AppResult.OK("新建默认地址成功");
        } else {
            usersAddress.setIsDefault(-1);
            usersAddress.setDataFlag(1);
            usersAddress.setCreateTime(new Date());
            xlsUsersAddressDao.insert(usersAddress);
            return  AppResult.OK("新建地址成功");
        }
    }

    @Override
    @Transactional
    public AppResult updateAddress(XlsUsersAddressEntity usersAddress1) {
        XlsUsersAddressEntity usersAddress = xlsUsersAddressDao.selectById(usersAddress1.getId());
        Integer isDefault = usersAddress.getIsDefault();
        if (isDefault == 1) {
            return AppResult.ERROR("默认地址不可更改");

        } else {
            xlsUsersAddressDao.updateById(usersAddress1);
            return AppResult.OK("更改成功");
        }
    }

    @Override
    public AppResult checkIsFirstAddress(Long userId) {
        XlsUsersAddressEntity usersAddress=new XlsUsersAddressEntity();
        usersAddress.setUserId(userId);
        List<XlsUsersAddressEntity> usersAddressList = xlsUsersAddressDao.selectUsersAddressList(usersAddress);
        if (usersAddressList.isEmpty()){
            return AppResult.OK("地址为空","1");
        }
        return AppResult.OK("地址不为空","0");
    }

    @Override
    public AppResult selectAddressByAddressId(Long id) {
        JSONObject  json=new JSONObject();
        XlsUsersAddressEntity usersAddress = xlsUsersAddressDao.selectById(id);
        String userName = usersAddress.getUserName();
        String userPhone = usersAddress.getUserPhone();
        Integer provinceId = usersAddress.getProvinceId();
        XlsAreasEntity areas = areasDao.selectById(provinceId);
        Integer cityId = usersAddress.getCityId();
        XlsAreasEntity areas1 = areasDao.selectById(cityId);
        Integer areaId = usersAddress.getAreaId();
        XlsAreasEntity areas2 = areasDao.selectById(areaId);
        String userAddressName = areas.getAreaName()+areas1.getAreaName()+areas2.getAreaName()+usersAddress.getUserAddress();
        json.put("addressId",id);
        json.put("userName",userName);
        json.put("userPhone",userPhone);
        json.put("userAddressName",userAddressName);
        return AppResult.OK("查询成功",json);
    }
}